package com.js.purs.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.js.basic.api.RemoteBasicService;
import com.js.basic.api.model.CodeRules;
import com.js.purs.enums.TableEnum;
import com.js.purs.mapper.PurProdInfoMapper;
import com.js.purs.mapper.PurProdPlanFileMapper;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.js.purs.mapper.PurProdBudgetMapper;
import com.js.purs.domain.PurProdBudget;
import com.js.purs.service.IPurProdBudgetService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

/**
 * 采购Service业务层处理
 *
 * @author lzq
 * @date 2024-06-12
 */
@Service
public class PurProdBudgetServiceImpl implements IPurProdBudgetService {
    @Autowired
    private PurProdBudgetMapper purProdBudgetMapper;

    @Resource
    private RemoteBasicService remoteBasicService;

    @Resource
    private PurProdInfoMapper purProdInfoMapper;


    /**
     * 查询采购
     *
     * @param bgId 采购主键
     * @return 采购
     */
    @Override
    public PurProdBudget selectPurProdBudgetByBgId(String bgId) {
        return purProdBudgetMapper.selectPurProdBudgetByBgId(bgId);
    }

    /**
     * 查询采购列表
     *
     * @param purProdBudget 采购
     * @return 采购
     */
    @Override
    public List<PurProdBudget> selectPurProdBudgetList(PurProdBudget purProdBudget) {
        return purProdBudgetMapper.selectPurProdBudgetList(purProdBudget);
    }

    /**
     * 新增采购
     *
     * @param purProdBudget 采购
     * @return 结果
     */
    @Override
    public int insertPurProdBudget(PurProdBudget purProdBudget) {
        return purProdBudgetMapper.insertPurProdBudget(purProdBudget);
    }

    /**
     * 修改采购
     *
     * @param purProdBudget 采购
     * @return 结果
     */
    @Override
    public int updatePurProdBudget(PurProdBudget purProdBudget) {
        return purProdBudgetMapper.updatePurProdBudget(purProdBudget);
    }

    /**
     * 批量删除采购
     *
     * @param bgIds 需要删除的采购主键
     * @return 结果
     */
    @Override
    public int deletePurProdBudgetByBgIds(String[] bgIds) {
        return purProdBudgetMapper.deletePurProdBudgetByBgIds(bgIds);
    }

    /**
     * 删除采购信息
     *
     * @param bgId 采购主键
     * @return 结果
     */
    @Override
    public int deletePurProdBudgetByBgId(String bgId) {
        return purProdBudgetMapper.deletePurProdBudgetByBgId(bgId);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPurProdBudgetBatch(List<PurProdBudget> purProdBudgets) {
        int i = purProdBudgetMapper.insertPurProdBudgetBatch(purProdBudgets);
        PurProdBudget budget = purProdBudgets.get(purProdBudgets.size() - 1);

        if (i <= 0) {
            return 0;
        }
        CodeRules code = remoteBasicService.getCodeByTableName(TableEnum.pur_prod_budget.getInfo());
        code.setEncodeNow(budget.getBgId());
        remoteBasicService.updateCodeNowByTableName(code);
        return i;
    }

    @Override
    public List<PurProdBudget> selectPurProdBudgetByPlanCode(String planCode) {
        List<PurProdBudget> budgetList = purProdBudgetMapper.selectPurProdBudgetByPlanCode(planCode);
        for (PurProdBudget budget : budgetList) {
            budget.setPurProdInfos(purProdInfoMapper.selectPurProdInfoByBgId(budget.getBgId()));
        }

        return budgetList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateOrInsertBudGet(List<PurProdBudget> budgetList) {
        boolean isSuccess = true;
        int code = 0;
        List<PurProdBudget> addList = new ArrayList<>();
        List<PurProdBudget> budgets = purProdBudgetMapper.selectPurProdBudgetList(new PurProdBudget());
        for (PurProdBudget budget : budgets) {
            for (PurProdBudget prodBudget : budgetList) {
                if (budget.getBgId().equals(prodBudget.getBgId())) {
                    //修改
                    addList.add(prodBudget);
                    isSuccess = purProdBudgetMapper.updatePurProdBudget(prodBudget) > 0 ? true : false;
                }
            }
        }
        if (isSuccess) {
            //新增
            budgetList.removeAll(addList);
            if (budgetList.size() > 0) {
                for (PurProdBudget budget : budgetList) {
                    code = purProdBudgetMapper.insertPurProdBudget(budget);
                }
            }
        }

        return 1;
    }

    @Override
    public int deleteBudGet(List<PurProdBudget> budGetList) {
        int code = 1;
        if (budGetList.size() > 0) {
            for (PurProdBudget budget : budGetList) {
                code = purProdBudgetMapper.deletePurProdBudgetByBgId(budget.getBgId());
            }
        }

        return 1;
    }


}
